{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "'''\n",
    "前言：在查资料时，发现模糊数学方法应用较为不广泛\n",
    "所以这一部分。。。。\n",
    "摆烂了。。。。\n",
    "'''"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b1与a的贴近度分别为： [0.8267949192431123, 0.8881966011250105, 0.8129171306613029]\n",
      "b2与a的贴近度分别为： [0.9292893218813453, 0.85, 0.9292893218813453]\n"
     ]
    }
   ],
   "source": [
    "#14.1\n",
    "\n",
    "'''\n",
    "b1应该归属于A2类\n",
    "b2应该归属于A1或者A2类皆可\n",
    "'''\n",
    "import numpy as np\n",
    "a = np.array([[0.2,0.4,0.5,0.1],\n",
    "              [0.2,0.5,0.3,0.1],\n",
    "              [0.2,0.3,0.4,0.1]])\n",
    "\n",
    "b1 = np.array([0,0.6,0.3,0.1])\n",
    "b2 = np.array([0.2,0.3,0.5,0])\n",
    "N1 = []\n",
    "N2 = []\n",
    "for i in range(len(a)):\n",
    "    N1.append(1-(np.linalg.norm(a[i]-b1)/2))\n",
    "    N2.append(1-(np.linalg.norm(a[i]-b2)/2))\n",
    "print('b1与a的贴近度分别为：',N1)\n",
    "print('b2与a的贴近度分别为：',N2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[  1.  -14.4 -14.1 -18. ]\n",
      " [-14.4   1.   -3.7  -7.8]\n",
      " [-14.1  -3.7   1.   -9.5]\n",
      " [-18.   -7.8  -9.5   1. ]]\n",
      "\n",
      "------------------------\n",
      " [[  1.  -14.1 -14.1 -14.1]\n",
      " [-14.1   1.   -3.7  -7.8]\n",
      " [-14.1  -3.7   1.   -7.8]\n",
      " [-14.1  -7.8  -7.8   1. ]]\n"
     ]
    },
    {
     "data": {
      "text/plain": "<Figure size 432x288 with 1 Axes>",
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAWAAAAD7CAYAAABUt054AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/YYfK9AAAACXBIWXMAAAsTAAALEwEAmpwYAAAFjElEQVR4nO3cMYulZx2H4f8TFmzEagMrajJ1cMQiIDaCaJciIAtCIMGtbPwIln4Jq2EV1MAgbLHWFoIKsyQ4kNpNDBlQm0Aqi9diIsqys5uV2blzzlwXDC+8c4ofh5n7PLwws7ZtGwCu3gv1AIDrSoABIgIMEBFggIgAA0RuPMuLb968uR0cHDynKQD76cGDB//Ytu3FR+8/U4APDg7m5OTk8lYBXANrrYePu+8RBEBEgAEiAgwQEWCAiAADRAQYICLAABEBBog80x9i7IJf/fn9uffuh/UM9sDr3/zKvPGtl+oZ7LG9OwHfe/fDee+jj+sZ7Lj3PvrYBznP3d6dgGdmXvnyl+btH3+7nsEO++HP/1hP4BrYuxMwwK4QYICIAANEBBggIsAAEQEGiAgwQESAASICDBARYICIAANEBBggIsAAEQEGiAgwQESAASICDBARYICIAANEBBggIsAAEQEGiAgwQESAASICDBARYICIAANEBBggIsAAEQEGiAgwQESAASICDBARYICIAANEBBggIsAAEQEGiAgwQESAASICDBARYICIAANEBBggIsAAEQEGiAgwQESAASICDBARYICIAANEBBggIsAAEQEGiAgwQESAASICDBARYICIAANEBBggIsAAEQEGiAgwQESAASICDBARYICIAANEBBggIsAAEQEGiAgwQESAASICDBARYICIAANEBBggIsAAEQEGiAgwQESAASICDBARYICIAANEbtQD+Bw7OZo5Pa5XNM5eP78e/azdUTm8PfPqnXrF3hNgLnZ6PHN2OnPrsF5y5d5+6V49oXN2en4V4OdOgHmyW4czd+7XK7hKR6/VC64Nz4ABIgIMEBFggIgAA0QEGCAiwAARAQaICDBARIABIgIMEBFggIgAA0QEGCAiwAARAQaICDBARIABIgIMEBFggIgAA0QEGCAiwAARAQaICDBARIABIgIMEBFggIgAA0QEGCAiwAARAQaICDBARIABIgIMEBFggIgAA0QEGCAiwAARAQaICDBARIABIgIMEBFggIgAA0QEGCAiwAARAQaICDBARIABIgIMEBFggIgAA0QEGCByox4A18bJ0czpcb3i6c7+cn49eq3d8Vkc3p559U694v/mBAxX5fR45uy0XvF0t75x/vV5d3a6Gx9oT+AEDFfp1uHMnfv1iv2wCyf0p3ACBogIMEBEgAEiAgwQEWCAiAADRAQYICLAABEBBogIMEBEgAEiAgwQEWCAiAADRAQYICLAABEBBogIMEBEgAEiAgwQEWCAiAADRAQYICLAABEBBogIMEBEgAEiAgwQEWCAiAADRAQYICLAABEBBogIMEBEgAEiAgwQEWCAiAADRAQYICLAABEBBogIMEBEgAEiAgwQEWCAiAADRAQYICLAABEBBogIMEBEgAEiAgwQEWCAiAADRAQYICLAABEBBogIMEBEgAEiAgwQEWCAiAADRAQYILK2bfvsL17r7zPz8PnNAdhLL2/b9uKjN58pwABcHo8gACICDBARYICIAANEBBggIsAAEQEGiOxdgNdaf11rfb/esas+ff9+utb6ZK31xcd8/5211k+KbbvsPz+Xa60frbX+UO/ZVRe9j7v6e793AeZS/Glm/jYzt//35lrr6zPzysz8uhgF+0aAucjdmXnrkXtvzczvtm37Z7AH9o4Ac5Ffzsx31lpfm5lZa70wM2/MeZiBSyDAPNa2bR/MzO9n5s1Pb31vZr4wM/erTbBvBJgnuTv/DfCbM/Obbdv+Fe6BvSLAPMlvZ+ara63vzswPxuMHuFQCzIW2bftkZo5n5mhmHm7bdhJPgr0iwDzN3Zl5eWZ+UQ+BfeMfsgNEnIABIgIMEBFggIgAA0QEGCAiwAARAQaICDBA5N+hzYDQpwI8SQAAAABJRU5ErkJggg==\n"
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "#14.2\n",
    "\n",
    "'''\n",
    "不理解为什么传递闭包之后数据出现负值\n",
    "'''\n",
    "'''\n",
    "1、依次输出相似矩阵和传递闭包\n",
    "1>-3.7>-7.8>-14.1\n",
    "当y=1时，被分为4类，{(1),(2),(3),(4)}\n",
    "当y=-3.7时，被分为3类，{(1),(2,3),(4)}\n",
    "当y=-7.8时，被分为2类，{(1),(2,3,4)}\n",
    "当y=-14.1时，被分为1类{(1,2,3,4)}\n",
    "'''\n",
    "#特性指标举证\n",
    "from numpy import array, zeros, triu\n",
    "import scipy.cluster.hierarchy as sch\n",
    "import matplotlib.pyplot as plt\n",
    "def hecheng(a,b):\n",
    "    m,N=a.shape; n=b.shape[1]; c=zeros((m,n))\n",
    "    for i in range(m):\n",
    "        for j in range(n):\n",
    "            c[i,j]=max([min(a[i,k],b[k,j]) for k in range(N)])\n",
    "    return c\n",
    "a = np.array([[38,12,16,13],\n",
    "              [73,74,22,64],\n",
    "              [86,49,27,68],\n",
    "              [58,64,84,63]])\n",
    "d=array([[sum(abs(a[i]-a[j])) for i in range(4)] for j in range(4)])\n",
    "r=1-0.1*d; print(r); tr=hecheng(r,r)\n",
    "while abs(r-tr).sum()>0.00001: r=tr; tr=hecheng(r,r)\n",
    "print('\\n------------------------\\n',tr)\n",
    "d2=1-tr  #为了画图，再次转换为距离关系\n",
    "d2=triu(d2,1); d2=d2[d2!=0] #提取矩阵上三角中的非零元素\n",
    "z=sch.linkage(d2); s=['I','II','III','IV']\n",
    "sch.dendrogram(z,labels=s)  #画聚类树\n",
    "plt.yticks([])  #y轴不可见\n",
    "plt.show()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "#14.3\n",
    "\n",
    "'''\n",
    "数据量大，输入计算机较为浪费时间。。。。\n",
    "在14章，本意是使用skfuzzy.cluster中的cmeans模块进行模糊聚类分析\n",
    "'''"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "#14.4\n",
    "\n",
    "#参考百度文库例4，https://wenku.baidu.com/view/af028dd907a1b0717fd5360cba1aa81145318f47.html\n",
    "'''\n",
    "由于大气中各污染物含量均是越少然后大气质量越高，可构造各污染物含量对四个等级对的隶属函数\n",
    "'''"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "#14.5\n",
    "#参考文档https://www.doc88.com/p-3147368976292.html\n",
    "#写的不错"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}